#-----------------------------------------------------------------------------
# Which parties drive the effects??
#-----------------------------------------------------------------------------
sink(file = "log_file3.txt", type = "output")

require(tidyverse) 
require(rio)      
require(magrittr)
require(dplyr)
require(stringr)
require(ggplot2)
require(tidyr)
require(MASS)
require(stargazer)
require(coefplot)
require(sjPlot)
require(margins)
require(plm)
require(estimatr)
require(lmtest)
require(lme4)
require(pscl)
require(jtools)
library(broom)     
library(rdrobust)   
library(rddensity)
library(rddtools)
library(huxtable)   
library(rdd)
require(haven)
require(plyr)
require(cowplot)
library(data.table)
require(gridExtra)
library(estimatr)
library(readr)
require(grid)
require(ggpubr)


################################################################################
#####RDD for each party family ######

#download data - please modify 
manifesto_pop<-read.csv('data.csv')

#creating variable for country FE
manifesto_pop$state.f = factor(manifesto_pop$countryname)
manifesto_pop$state.d = model.matrix(~manifesto_pop$state.f+0)

#revalue party family following Manifeso's classification
manifesto_pop$parfam<-revalue(as.factor(manifesto_pop$parfam),c("80"="Agrarian","10"="Ecological","20"="Socialist","30"="Social Democrat","40"="Liberal","50"="Christian Democrat","60"="Conservative","70"="Nationalist","90"="Ethnic","95"="Special Issue" ,"98"=NA,"999"=NA))

eco<-subset(manifesto_pop, parfam=="Ecological")
soc<-subset(manifesto_pop,parfam=="Socialist") 
sd<-subset(manifesto_pop, parfam=="Social Democrat") 
lib<-subset(manifesto_pop, parfam=="Liberal")
cd<-subset(manifesto_pop, parfam=="Christian Democrat")
con<-subset(manifesto_pop, parfam=="Conservative")
nat<-subset(manifesto_pop, parfam=="Nationalist")
agr<-subset(manifesto_pop,parfam =="Agrarian")
eth<-subset(manifesto_pop,parfam=="Ethnic") #ethnic party 
spe<-subset(manifesto_pop, parfam=="Special Issue")

left<-subset(manifesto_pop, parfam=="Socialist"|parfam=="Social Democrat") 
right<-subset(manifesto_pop, parfam=="Christian Democrat"|parfam=="Conservative") 
right2<-subset(manifesto_pop, parfam=="Nationalist"|parfam=="Ethnic")


#-----------------------------------------------------------------------------
# Left (aggregated) Table 2 
#-----------------------------------------------------------------------------
#Table 2 
left$military_l<-log(left$military+1)
left1<-left%>%dplyr::select(dif_l1, military_l,countryname,state.d, treatment)
left1<-left1[complete.cases(left1), ]
leftfuzzy <- rdd_data(x=left1$dif_l1, y=left1$military_l,z=left1$treatment,covar=cbind(left1$state.d),cutpoint=0)
leftfuzzy2 <- rdd_data(x=left1$dif_l1, y=left1$military_l,z=left1$treatment,cutpoint=0)

#parametric fuzzy
summary(lefti<-rdd_reg_lm(rdd_object= leftfuzzy, covar.opt = list(strategy = c("include"), slope = c( "separate")), order=1))
coeftest(lefti, vcov=vcovHC(lefti,type="HC0",cluster="countryname"))
summary(lefti2<-rdd_reg_lm(rdd_object= leftfuzzy2, order=1))
coeftest(lefti2, vcov=vcovHC(lefti2,type="HC0",cluster="countryname"))

#nonparametric fuzzy
summary(rdbwselect_2014(y = left1$military_l, x = left1$dif_l1, c=0, bwselect="CCT"))
# with Fixed effect
summary(rdrobust(y = left1$military_l, x = left1$dif_l1, c=0, kernel = "tri",level = 95, p=2, fuzzy =left1$treatment, h=2.233, b=3.8987,covs=cbind(left1$state.d), cluster=left1$countryname, all=TRUE))
# without Fixed Effect
summary(rdrobust(y = left1$military_l, x = left1$dif_l1, c=0, kernel = "tri",level = 95, p=2, fuzzy = left1$treatment, h=2.233, b=3.8987, cluster=left1$countryname, all=TRUE))

#plot (Figure 2)
le<-left1%>%dplyr::filter(dif_l1>-5, dif_l1<5)
pdf("Fig.2.pdf")
rdplot(x=le$dif_l1, y=le$military_l, c=0, p=2, covs=le$state.d, x.label="Difference between RRPP vote and threshold (t-1)", y.label="Position of National Defence" , title="Socialist & Social Democrat",y.lim = c(-1,2))
dev.off()
#-----------------------------------------------------------------------------
# Right (Table 3)
#-----------------------------------------------------------------------------
right$military_l<-log(right$military+1)
right1<-right%>%dplyr::select(dif_l1, military_l,countryname,state.d, treatment)
right1<-right1[complete.cases(right1), ]
rightfuzzy <- rdd_data(x=right1$dif_l1, y=right1$military_l,z=right1$treatment,covar=cbind(right1$state.d),cutpoint=0)
rightfuzzy2 <- rdd_data(x=right1$dif_l1, y=right1$military_l,z=right1$treatment,cutpoint=0)

#parametric fuzzy
# with Fixed Effect
summary(righti<-rdd_reg_lm(rdd_object= rightfuzzy, covar.opt = list(strategy = c("include"), slope = c( "separate")), order=1))
coeftest(righti, vcov=vcovHC(righti,type="HC0",cluster="countryname"))
# without Fixed Effect
summary(righti2<-rdd_reg_lm(rdd_object= rightfuzzy2, order=1))
coeftest(righti2, vcov=vcovHC(righti2,type="HC0",cluster="countryname"))

#nonparametric fuzzy
summary(rdbwselect_2014(y = right1$military_l, x = right1$dif_l1, c=0, bwselect="CCT"))
# with FE
summary(rdrobust(y = right1$military_l, x = right1$dif_l1, c=0, kernel = "tri",level = 95, p=2, fuzzy =right1$treatment, h=1.910, b=3.193,covs=cbind(right1$state.d), cluster=right1$countryname, all=TRUE))
# without FE
summary(rdrobust(y = right1$military_l, x = right1$dif_l1, c=0, kernel = "tri",level = 95, p=2, fuzzy = right1$treatment, h=1.910, b=3.193, cluster=right1$countryname, all=TRUE))



sink()
